package com.mpp.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mpp.common.result.Result;
import com.mpp.common.util.AuthContextHolder;
import com.mpp.common.util.StringUtils;
import com.mpp.model.entity.user.UserInfo;
import com.mpp.model.vo.user.LoginVo;
import com.mpp.model.vo.user.UserAuthVo;
import com.mpp.model.vo.user.UserInfoQueryVo;
import com.mpp.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName : UserInfoController  //类名
 * @Description :   //描述
 * @Author : 易嵘  //作者
 * @Date: 2022-08-16 14:48  //时间
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/user/userInfo")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public Result register(@RequestBody LoginVo loginVo){
        return userInfoService.registerUser(loginVo)? Result.ok():Result.fail();
    }


    /**
     * 条件查询贷分页
     * @param limit 显示行数
     * @param current 当前页数
     * @param userInfoQueryVo 查询模型
     * @return 查询结果
     */
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPageUserInfo/{current}/{limit}")
    public Result findPageUserInfo(@PathVariable long limit, @PathVariable long current, @RequestBody(required = false) UserInfoQueryVo userInfoQueryVo){
        Page<UserInfo> page = new Page<>(current, limit);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();

        String keyword = userInfoQueryVo.getKeyword();
        Integer status = userInfoQueryVo.getStatus();
        Integer authStatus = userInfoQueryVo.getAuthStatus();
        String createTimeBegin = userInfoQueryVo.getCreateTimeBegin();
        String createTimeEnd = userInfoQueryVo.getCreateTimeEnd();

        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like("nick_name", keyword).or().like("name",keyword);
        }
        if (status!=null) {
            wrapper.eq("status", status);
        }
        if (authStatus!=null) {
            wrapper.eq("auth_status", authStatus);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time", createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le("create_time", createTimeEnd);
        }
        return Result.ok(userInfoService.page(page, wrapper));
    }

    /**
     * 查询所有用户信息
     * @return 查询结果
     */
    @ApiOperation(value = "查询所有用户")
    @GetMapping("finAll")
    public Result findAllUserInfo(){
        return Result.ok(userInfoService.list());
    }

    @ApiOperation(value = "根据ID查询用户信息")
    @GetMapping("getUserInfo/{id}")
    public Result getUserInfo(@PathVariable String id){
        return Result.ok(userInfoService.getById(id));
    }

    @ApiOperation(value = "新增用户信息")
    @PostMapping("saveUserInfo")
    public Result saveUserInfo(@RequestBody UserInfo userInfo){
        return userInfoService.save(userInfo)? Result.ok():Result.fail();
    }

    @ApiOperation(value = "修改用户心")
    @PostMapping("updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfo userInfo){
        return userInfoService.updateById(userInfo)? Result.ok():Result.fail();
    }

    @ApiOperation(value = "batchRemove")
    @DeleteMapping("batchRemove")
    public Result batchRemoveUserInfo(@RequestBody List<Long> listId){
        return userInfoService.removeByIds(listId)? Result.ok():Result.fail();
    }

    @ApiOperation(value = "根据id删除用户信息")
    @DeleteMapping("{id}")
    public Result removeUserInfo(@PathVariable Long id){
        return userInfoService.removeById(id)? Result.ok():Result.fail();
    }

    /**
     * 会员认证
     * @param userAuthVo 会员认证模型
     * @param request http请求
     * @return
     */
    @ApiOperation(value = "会员认证")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        return userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo) ? Result.ok():Result.fail();
    }

    /**
     * 获取会员信息
     * @param request http请求
     * @return 当前用户信息
     */
    @ApiOperation(value = "获取会员信息")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        return Result.ok(userInfoService.getById(userId));
    }
}
